package ao.util.text;

import java.text.DecimalFormat;

/**
 * User: alex
 * Date: 23-Apr-2009
 * Time: 3:18:48 PM
 */
public class AoFormat
{
    //--------------------------------------------------------------------
    private AoFormat() {}


    //--------------------------------------------------------------------
    private static final DecimalFormat DECIMAL_FORMAT =
            new DecimalFormat("#,###");


    //--------------------------------------------------------------------
    public static String hhmmss(long millis)
    {
        int  hour    = 1000 * 60 * 60;
        long hours   = millis / hour;
        int  minute  = 1000 * 60;
        long minutes = (millis % hour) / minute;
        int  second  = 1000;
        long seconds = ((millis % hour) % minute) / second;

        StringBuilder str = new StringBuilder();
        if (hours > 0) {
            str.append( hours ).append(":")
               .append( twoDigit(minutes) ).append(":")
               .append( twoDigit(seconds) );
        } else {
            if (minutes > 0) {
                str.append( minutes ).append(":")
                   .append( twoDigit(seconds) );
            } else {
                if (seconds > 0) {
                    str.append( seconds ).append(" s");
                } else {
                    str.append( millis  ).append(" ms");
                }
            }
        }
        return str.toString();
    }

    public static String twoDigit(long value)
    {
        return (value < 10)
               ? "0" + value
               : Long.toString(value);
    }


    //--------------------------------------------------------------------
    public static String decimal(long value)
    {
        return DECIMAL_FORMAT.format(value);
    }
}
